package com.qf.service.impl;

import com.qf.cache.CategoryCache;
import com.qf.events.CategoryChangeEvent;
import com.qf.exeptions.AddDataException;
import com.qf.exeptions.DeleteDataException;
import com.qf.exeptions.QueryDataException;
import com.qf.exeptions.ResultCode;
import com.qf.mappers.CategoryMapper;
import com.qf.pojo.Category;
import com.qf.pojo.CheckType;
import com.qf.pojo.User;
import com.qf.service.CategoryService;
import com.qf.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*
杨威
2021/12/31
20:17
*/
@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    ApplicationContext ac;
    @Autowired
    CategoryCache categoryCache;
    @Autowired
    CategoryMapper categoryMapper;
    @Override
    public void addCategory(Category category) {
        Assert.isTrue(!category.isEmpty(CheckType.ADD),()->{
            throw new AddDataException("数据不完整", ResultCode.CATEGORY_DATA_NULL);
        });
        Category category1 = categoryMapper.selectByName(category.getCategoryName());
        Assert.isTrue(ObjectUtils.isEmpty(category1),()->{
            throw new AddDataException("分类已经存在",ResultCode.DATA_ALREADY_EXIST);
        });
        category.setCreateTime(new Date());
        User user = SecurityUtils.getUser();
        category.setCreateBy(user.getUsername());
        categoryMapper.addCategory(category);
        ac.publishEvent(new CategoryChangeEvent());
    }

    @Override
    public int updateCategory(Category category) {
        Assert.isTrue(!category.isEmpty(CheckType.UPDATE),()->{
            throw new AddDataException("数据不完整", ResultCode.CATEGORY_DATA_NULL);
        });
        //查一下要更新的数据在不在 不在的话抛一个异常
        Category category1 = categoryMapper.selectById(category.getCId());
        Assert.isTrue(!ObjectUtils.isEmpty(category1),()->{
            throw new AddDataException("分类不存在",ResultCode.CATEGORY_DATA_NULL);
        });
        category.setCreateTime(new Date());
        User user = SecurityUtils.getUser();
        category.setCreateBy(user.getUsername());
        int i = categoryMapper.updateCategory(category);
        ac.publishEvent(new CategoryChangeEvent());
        return i;
    }

    @Override
    public Category selectById(Long cId) {
        Assert.isTrue(cId>0,()->{
            throw new QueryDataException("主键超出允许的范围",ResultCode.ID_NOT_ALLOWED);
        });
        Category category = categoryCache.getValue(cId);
        Assert.isTrue(!ObjectUtils.isEmpty(category),()->{
            throw new QueryDataException("数据不存在",ResultCode.ID_NOT_ALLOWED);
        });
        return category;
    }

    @Override
    public Category selectByName(String cName) {
        return null;
    }

    @Override
    public List<Category> selectAllCategories() {
        List<Category> allData = categoryCache.getAllData();
        return allData;
    }

    @Override
    public List<Category> selectAllEnableCategories() {
        return null;
    }

    @Override
    public List<Category> selectCategoriesByNameLike(String cName) {
        Assert.hasText(cName,()->{
            throw new QueryDataException("分类名不能为空",ResultCode.QUERY_PARAM_EMPTY);
        });
        List<Category> categoryList = categoryCache.getAllData();
        List<Category> list= new ArrayList();
        for (Category category : categoryList) {
            if(category.getCategoryName().contains(cName)){
                list.add(category);
            }
        }
        return list;
    }

    @Override
    public int deleteCategoriesByIds(List<Long> ids) {

        Assert.notEmpty(ids,()->{
            throw new DeleteDataException("没有传递主键",ResultCode.ID_NOT_ALLOWED);
        });
        int i = categoryMapper.deleteCategoriesByIds(ids);
        ac.publishEvent(new CategoryChangeEvent());
        return i;
    }
}
